<template>
	<view class="content">
		
		<view class="user-box">
			<m-tree ref="mtree" :defaultProps="defaultProps" :isGroup='false' :isUser="true" :data="tree"
				:divider="true" :unfold="true" @node-click="nodeClick">
			</m-tree>
		</view>
		
		<view class="empty-data" v-if="tree.length==0">
			<u-empty mode="data" :marginTop="100"></u-empty>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				defaultProps: {
					id: 'id', // 此项为id项的key
					children: 'children', // 此项为修改子集数据的key
					label: 'deptName' // 此项为修改显示数据的key
				},
				tree: [],
			}
		},
		onLoad() {
			this.getUnitData()
		},
		methods: {
			getUnitData() {
				uni.$u.http.get(`/base/mobile/sysDeptInfo/deptTreeList`).then(res => {
					if (res.code == 0) {
						let newData = this.format(res.data)
						this.tree = newData
					}
				}).catch(err => {})
			},
			format(arr) {
				arr.map(item => {
					item.checked = false
					if (!item.children) {
						item.children = []
					} else if (item.children) {
						item.children = this.format(item.children)
					}
				})
				return arr
			},
			setStatus(e, arr) {
				arr.map(item => {
					if (item.id == e.id) {
						item.checked = !item.checked
					}
					if (item.children.length) {
						item.children = this.setStatus(e, item.children)
					}
				})
				return arr
			},
			nodeClick(e) {
				// 多选
				// if (!e.children.length && e.type == 2) {
				// 	this.setStatus(e, this.tree)
				// }
				// 单选
				if (!e.children.length) {

					uni.$emit('setBumen', e)
					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
						delta: 1
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #ededed;
	}

	.help-pubuBox {
		padding: 22rpx;
		background: #fff;
	}

	.user-box {
		background-color: #fff;
		padding-bottom: 50rpx;
	}

	.search-box {
		background-color: #fff;
		padding: 0 22rpx;

		.main-box {
			display: flex;
			align-items: center;
			border-bottom: 1px solid #f2f2f2;
			font-size: 30rpx;
			padding: 16rpx 50rpx;

			.edit-icon {
				width: 40rpx;
				height: 40rpx;
				padding-right: 10rpx;
			}
		}
	}
</style>
